import {Component, OnInit, ViewEncapsulation, ViewChild} from '@angular/core';
import {Content} from "@shared/components/toolbar/toolbar-content";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {ActivatedRoute, Params, Router} from "@angular/router";
import {GoodsService} from "@goods/goods.service";

@Component({
  selector: 'seed-goods-list',
  templateUrl: './goods-list.component.html',
  styleUrls: ['./goods-list.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class GoodsListComponent implements OnInit {
  public store = {
    goods: [],
    goodsTypeTitle: '票务',
    hasData: true
  };
  public params = {
    status: 0,
    goodsCategoryCode: '',
    page: 1,
    rows: 10,
  };
  @ViewChild(Content) content;
  @ViewChild(SeedInfiniteScroll) infiniteScroll;

  constructor(private goodsService: GoodsService,
              public routerTo: Router,
              private router: ActivatedRoute) {
    this.router.params.subscribe((params: Params) => {
      this.params.goodsCategoryCode = params['goodsCategoryCode'];
    });
    this.router.queryParams.subscribe((params: Params) => {
      if (params['title']) { this.store.goodsTypeTitle = decodeURI(params['title']); };
    });
  }

  /**
   * 获取商品列表
   */
  getGoosList() {
    this.goodsService.getGoodsList(this.params).subscribe(res => {
      if (res.result === '0' && res.data) {
        this.params.page++;
        if (this.infiniteScroll) {
          this.infiniteScroll.complete();
        }
        this.store.goods = this.store.goods.concat(res.data.list);
        if (this.store.goods.length === 0) {
          this.store.hasData = false;
        }
        if (!res.data.hasNextPage && this.infiniteScroll) {
          this.infiniteScroll.enable(false);
        }
      }
    });
  }

  /**
   * 前往商品详情
   * @param goodsCode 商品编码
   */
  goToGoodsDetailPage(goodsCode: string) {
    if (goodsCode) {
      this.routerTo.navigate(['/goods/detail', goodsCode ]);
      /**{queryParams: {goodsCode: goodsCode}}*/
    }
  }

  // /**
  //  * @description 获取商品列表筛选
  //  * @param goodsCategoryCode 商品类型编码
  //  */
  // getListFilter(goodsCategoryCode: GoodsResp) {
  //   this.goodsService.getListFilter(goodsCategoryCode).subscribe((res: SNSResult<GoodsFilterResp | any>) => {
  //     let filterObj = res.data;
  //     if (res.result == '0' && res.data) {
  //       let all = {
  //         cateCode: this.goodsCategoryCode,
  //         cateName: '全部'
  //       };
  //       if(filterObj['categoryList']){
  //         filterObj['categoryList'].unshift(all);
  //         let store = this.tools.setKeys(filterObj);
  //         this.filterData = [...store];
  //       }
  //     }
  //   })
  // }

  // /**
  //  * 通过过滤器筛选商品
  //  * @param e
  //  */
  // onSelected(e) {
  //   this.params['page'] = 1;
  //   this.store['goods'] = [];
  //   let cateCode = e ? e.cateCode : this.goodsCategoryCode;
  //   this.params['goodsCategoryCode'] = cateCode;
  //   this.getGoosList();
  //   this.content.scrollTop();
  // }
  ngOnInit() {
    this.getGoosList();
  }
}
